﻿using browser4gp.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace browser4gp.Views
{
    /// <summary>
    /// Interaction logic for BrowserView.xaml
    /// </summary>
    public partial class BrowserView : Window, IDisposable
    {

        /// <summary>
        /// Costruttore
        /// </summary>
        /// <param name="model">Model per gestire la videata</param>
        public BrowserView(BrowserViewModel model)
        {
            DataContext = model;
            InitializeComponent();
        }


        private BrowserViewModel Model 
        { 
            get
            {
                return DataContext as BrowserViewModel;
            }
        }

        private void WebControl_LoadingFrame(object sender, Awesomium.Core.LoadingFrameEventArgs e)
        {
            Model.LoadingFrameStart(sender, e);
        }

        private void WebControl_LoadingFrameComplete(object sender, Awesomium.Core.FrameEventArgs e)
        {
            Model.LoadingFrameComplete(sender, e);
        }

        private void WebControl_LoadingFrameFailed(object sender, Awesomium.Core.LoadingFrameFailedEventArgs e)
        {
            Model.LoadingFrameFailed(sender, e);
        }

        private void WebControl_TitleChanged(object sender, Awesomium.Core.TitleChangedEventArgs e)
        {
            Model.Title = e.Title;
        }



        /// <summary>
        /// Libero le risorse
        /// </summary>
        public void Dispose()
        {
            if (webControl != null &&
                webControl.WebSession != null)
            {
                webControl.WebSession.ClearCache();
                webControl.WebSession.ClearCookies();
                webControl.Dispose();
                webControl = null;
            }
        }
    }
}
